package net.gfbridge.demo.client;

import bridge.FABridge;
import bridge.FABridgeInstance;
import bridge.FABridgeWrapper;
import bridge.InitCallback;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.Dictionary;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import flash.events.AS3EventHandler;
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.events.SecurityErrorEvent;
import flash.net.FileReferenceWrapper;
import flash.net.URLRequest;
import flash.wrapper.upload.UploadButton;
import flash.wrapper.upload.UploadRoot;

public class SimpleUpload implements EntryPoint {
    public static int maxMSize = 1;
    public static int maxFileSize = maxMSize * 1024 * 1024;
    String bridgeName = "uploadBtn";

    String uploadUrl;
    URLRequest uploadReq;
    UploadButton uploadBtn;
    FileReferenceWrapper file;

    boolean uploading = false;
    

    public void onModuleLoad() {
        uploadUrl = Dictionary.getDictionary("config").get("uploadUrl");
        String base = GWT.getModuleBaseURL();
        uploadBtn = new UploadButton(base + "default_upload_button.swf", bridgeName, "60px", "30px") {

            @Override
            public void buttonInitComplete() {
                addHandler(new AS3EventHandler() {
                    public void onEvent(Event arg0) {
                        if (!uploading) {
                            clear();
                            file = new FileReferenceWrapper(bridgeName, true);
                            DemoHandler handler = new DemoHandler();
                            file.addHandler(handler, Event.SELECT);
                            file.addHandler(handler, Event.COMPLETE);
                            file.addHandler(handler, DataEvent.UPLOAD_COMPLETE_DATA);
                            file.addHandler(handler, IOErrorEvent.IO_ERROR);
                            file.addHandler(handler, SecurityErrorEvent.SECURITY_ERROR);
                            file.browse();
                        } else {
                            Window.alert("i'm busy!");
                        }
                    }
                }, MouseEvent.CLICK);
            }
        };
        uploadBtn.setBridgeName(bridgeName);
        uploadBtn.setButtonName("Upload");

        RootPanel.get("simple-upload-demo").add(uploadBtn);
    }
    // clear as3 object reference
    void clear(){
        if(file!=null){
            FABridge.get().getInstance(bridgeName).releaseNamedASObject(file.getNativeAS3Obj());
        }
        if(uploadReq!=null){
            FABridge.get().getInstance(bridgeName).releaseNamedASObject(uploadReq);
        }
        file = null;
        uploadReq = null;

    }
    class DemoHandler implements AS3EventHandler {
        public void onEvent(Event event) {
            String type = event.getType();
            if (Event.SELECT.equals(type)) {
                int size = (int) file.getSize();
                if (size > maxFileSize) {
                    Window.alert("file is too large, maxsize: " + maxMSize + "M (" + maxFileSize + " bytes)");
                    return;
                }

                uploadReq = URLRequest.createURLRequest(bridgeName);
                uploadReq.setUrl(uploadUrl);
                uploadReq.setMethod("POST");

                file.upload(uploadReq, "upload_test");
            }
            if (Event.COMPLETE.equals(type)) {
                file = null;
                uploading = false;
                Window.alert("upload complete");
            }
            if (DataEvent.UPLOAD_COMPLETE_DATA.equals(type)) {
                String data = ((DataEvent) event).getData();
                Window.alert("server response: " + data);
            }
            if (IOErrorEvent.IO_ERROR.equals(type)) {
                Window.alert("io error");
            }

            if (SecurityErrorEvent.SECURITY_ERROR.equals(type)) {
                Window.alert("security error");
            }

        }
    }
}
